package com.play.ballen.me.ui

import android.content.Context
import android.content.Intent
import com.hjq.toast.ToastUtils
import com.play.ballen.R
import com.play.ballen.databinding.ActivityRegisterBinding
import com.play.ballen.home.ui.MainActivity
import com.play.ballen.me.dialog.PrivacyDialogUtils
import com.play.ballen.me.vm.RegisterVM
import com.yuven.appframework.base.BaseStdTitleActivity
import com.yuven.appframework.manager.AppActivitysManager
import com.yuven.appframework.util.setOnSingleClickListener
import com.yuven.baselib.utils.ResourceUtil

class RegisterActivity : BaseStdTitleActivity<RegisterVM, ActivityRegisterBinding>() {

    companion object {
        @JvmStatic
        fun start(context: Context) {
            val starter = Intent(context, RegisterActivity::class.java)
            context.startActivity(starter)
        }
    }

    private val countDownTip by lazy { ResourceUtil.getString(R.string.count_down_time_tip) }
    private val privacyDialogUtils by lazy { PrivacyDialogUtils(this) }

    override fun viewModelClass(): Class<RegisterVM> {
        return RegisterVM::class.java
    }

    override fun contentLayoutId(): Int {
        return R.layout.activity_register
    }

    override fun initObserver() {
        super.initObserver()
        viewModel.imLoginStatus.observe(this){
            if (it==1){
                AppActivitysManager.getInstance().finishAllActivity()
                MainActivity.start(this)
            }else{
                if(viewModel.registerSuccess.value==1){
                    ToastUtils.show("注册成功")
                    AppActivitysManager.getInstance().finishAllActivity()
                    LoginActivity.start(this)
                }
            }
        }
        viewModel.actionPrivacy.observe(this) {
            it?.let {
                privacyDialogUtils.show(it)
            }
        }

        viewModel.actionCountDown.observe(this) {
            dataBinding.tvGetCode.isClickable = false
            dataBinding.tvGetCode.isEnabled = false
            dataBinding.tvGetCode.text = it.toString() + countDownTip
        }

        viewModel.actionCountDownComplete.observe(this) {
            dataBinding.tvGetCode.isClickable = true
            dataBinding.tvGetCode.isEnabled = true
            dataBinding.tvGetCode.text = ResourceUtil.getString(R.string.register_code_get)
        }
    }

    override fun initData() {
        dataBinding.tvGetCode.setOnSingleClickListener tag@{
            val account = dataBinding.edAccount.text.toString().trim()
            if (account.isEmpty()) {
                ToastUtils.show(dataBinding.edAccount.hint)
                return@tag
            }
            viewModel.sendCode(account)
        }

        dataBinding.tvPrivacy.setOnSingleClickListener tag@{
            viewModel.getPrivacy()
        }

        dataBinding.btnRegister.setOnSingleClickListener tag@{
            val account = dataBinding.edAccount.text.toString().trim()
            if (account.isEmpty()) {
                ToastUtils.show(dataBinding.edAccount.hint)
                return@tag
            }

            val code = dataBinding.edCode.text.toString().trim()
            if (code.isEmpty()) {
                ToastUtils.show(dataBinding.edCode.hint)
                return@tag
            }

            val pwd = dataBinding.edPwd.text.toString().trim()
            if (pwd.isEmpty()) {
                ToastUtils.show(dataBinding.edPwd.hint)
                return@tag
            }

            val pwdConfirm = dataBinding.edPwdConfirm.text.toString().trim()
            if (pwdConfirm.isEmpty()) {
                ToastUtils.show(dataBinding.edPwdConfirm.hint)
                return@tag
            }

            if (pwdConfirm != pwd) {
                ToastUtils.show(ResourceUtil.getString(R.string.register_pwd_not_equal))
                return@tag
            }

            val codeInvite = dataBinding.edCodeInvite.text.toString().trim()
            if (codeInvite.isEmpty()) {
                ToastUtils.show(dataBinding.edCodeInvite.hint)
                return@tag
            }

            if (!dataBinding.checkbox.isChecked) {
                ToastUtils.show(ResourceUtil.getString(R.string.register_read_privacy_tip))
                return@tag
            }

            viewModel.register(account, pwd, code, codeInvite)
        }
    }

    override fun initIntent(intent: Intent?) {
    }

    override fun getToolBarTitle(): String = ""
}